<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/30 0030
 * Time: 上午 9:55
 */

namespace app\admin\controller;

use app\admin\model\ProductTypes as A;
use app\admin\model\Product as B;
use app\admin\model\Place as C;
class Product extends  Common
{
    private $PageTitle ;
    //实例化model
    protected $db;
    protected function _initialize()
    {
        parent::_initialize();
        $this->db= new B();
    }
    public function index()
    {
        $allData=$this->db->getAllData(1);
        $this->assign('allData',$allData);
        $this->PageTitle='商品管理';
        $this->generalInfor($this->PageTitle);
        return $this->fetch();
    }
    public function edit()
    {
        if(request()->isPost())
        {
            //halt($_POST);
            $res=$this->db->edit_goods(input('post.'));
            $mess=array(
                'status'=>$res['valid'],
                'message'=>$res['msg']
            );
            return $mess;
        }
        $products= (new B())->getProducts(input('param.id'));

        $type= new A();
        $arr=$type->getTypeDataAll();
        $this->assign('arr',$arr);
        $this->PageTitle='商品添加';
        $this->generalInfor($this->PageTitle);
        $this->assign('products',$products);

        return $this->fetch();
    }
    public function add_goods()
    {
        $type= new A();
        $arr=$type->getTypeDataAll();
        $this->assign('arr',$arr);
        $this->PageTitle='商品添加';
        $this->generalInfor($this->PageTitle);
        if(request()->isPost())
        {
            //halt($_POST);
            $res=$this->db->add_goods(input('post.'));
            $mess=array(
                'status'=>$res['valid'],
                'message'=>$res['msg']
            );
            return $mess;
        }
        return $this->fetch();
    }

    public function place()
    {
        $place= new C();
        $allData=$place->getAllPlace(1);
        $this->assign('allData',$allData);
        $this->PageTitle='商品发货地';
        $this->generalInfor($this->PageTitle);
        return $this->fetch();
    }
    public function edit_place()
    {
        if(request()->isPost())
        {
            //halt($_POST);
            $place= new C();
            $res=$place->add_place(input('post.'));
            $mess=array(
                'status'=>$res['valid'],
                'message'=>$res['msg']
            );
            return $mess;
        }
        $id=input('param.id');
        $res=(new C())->getIdPlace($id);
        //halt($res);
        $this->assign('data',$res);
        $this->PageTitle='发货地址修改';
        $this->generalInfor($this->PageTitle);
        return $this->fetch();
    }
    public function add_place()
    {

        if(request()->isPost())
        {
            //halt($_POST);
            $place= new C();
            $res=$place->add_place(input('post.'));
            $mess=array(
                'status'=>$res['valid'],
                'message'=>$res['msg']
            );
            return $mess;
        }
        $pid=input('param.pid');
        $this->assign('pid',$pid);
        $this->PageTitle='发货地址添加';
        $this->generalInfor($this->PageTitle);
        return $this->fetch();
    }
    public function specific()
    {
        $this->PageTitle = '商品规格元素';
        $this->generalInfor($this->PageTitle);
        $productsData=$this->db->getProductName(input('param.pid'),'products_name');
        //$this->redirect('Product/product_specific', ['pid' => input('param.pid')]);
        //halt($productsData);
        /*if($productsData['products_specifications'] && $productsData['products_specifi']=="")
        {
            $this->redirect('Product/product_specific', ['pid' => $productsData['products_id']]);
        }else if($productsData['products_specifications'] && $productsData['products_specifi']!="")
            {
                $this->redirect('Product/product_specific_index', ['pid' => $productsData['products_id']]);
            }*/
        $this->assign('productsData',$productsData);
        if (request()->isPost()) {
            //halt($_POST);
            $res = $this->db->add_specific(input('post.'));
            $mess = array(
                'status' => $res['valid'],
                'message' => $res['msg']
            );
            return $mess;
        }
        return $this->fetch();

    }
    public function product_specific_index()
    {
        $this->PageTitle = '商品规格展示';
        $this->generalInfor($this->PageTitle);
        $products_id=input('param.pid');
        $productsData=$this->db->getProductName(input('param.pid'),'products_name');
        $SpecifcData=$this->db->getSpecifiIndex($products_id);
        $this->assign('productsData',$productsData);
        $this->assign('SpecifcData',$SpecifcData);
        //halt($SpecifcData);
        return $this->fetch();

    }
    public function product_specific()
    {
        $this->PageTitle = '商品规格';
        $this->generalInfor($this->PageTitle);
        $products_id=input('param.pid');
        $SpecifcData=$this->db->getSpecifiData($products_id);
        $this->assign('SpecifcData',$SpecifcData);
         $this->assign('products_id',$products_id);
        if (request()->isPost()) {
            //halt($_POST);
            $res = $this->db->add_prdspecific(input('post.'));
            $mess = array(
                'status' => $res['valid'],
                'message' => $res['msg']
            );
            return $mess;
        }
        return $this->fetch();
    }
    //修改规格价格
    public function editSpr()
    {
        $res=$this->db->editSpr(input('param.'),'pspeci_price');
            $mess = array(
                'status' => $res['valid'],
                'message' => $res['msg']
            );
        return $mess;
    }
    public function editStock()
    {
        $res=$this->db->editSpr(input('param.'),'pspeci_stock');
        $mess = array(
            'status' => $res['valid'],
            'message' => $res['msg']
        );
        return $mess;
    }
    //删除
    public function deleteInfo()
    {
        $res=$this->db->deleteInfo(input('param.'));
        $mess = array(
            'status' => $res['valid'],
            'message' => $res['msg']
        );
        return $mess;
    }
    //下架
    public function shelf()
    {
        $res=$this->db->shelf(input('param.'));
        $mess = array(
            'status' => $res['valid'],
            'message' => $res['msg']
        );
        return $mess;
    }
    //获得分类下的产品信息
    public function getTypeProducts()
    {
     $res=$this->db->getTypeProducts(input('param.'));
     return $res;
    }
    //下架原因提交
    public  function reasonPost()
    {
        $res=$this->db->updateReason(input('param.'));
        return $res;
    }
    //发布提交
    public function faShow()
    {
        $res=$this->db->faShow(input('param.id'));
        return $res;
    }
}